#include<cstdio>
#include<semaphore.h>
#include<pthread.h>
#include<vector>
#include<cstdlib>
#include<unistd.h>
sem_t full,empty,mutex;
using namespace std;
vector<int>v;
void*producer(void*i)
{
	while(true)
	{
	
	int temp=rand()%10;
	printf("\nproducer produced item %d\n",temp);
	sem_wait(&empty);
	sem_wait(&mutex);
	printf("producer put item %d\n",temp);
	v.push_back(temp);
	sem_post(&mutex);
	sem_post(&full);	
	}
}	
void*consumer(void*i)
{
	while(true)
	{
	sem_wait(&full);
	sem_wait(&mutex);
	int temp=v.back();
	printf("consumer got item %d\n",temp);
	v.pop_back();
	sem_post(&mutex);
	sem_post(&empty);
	printf("consumer consumed item %d\n",temp);
	}
}	

int main()
{
	pthread_t prod,cons,i=0;
	pthread_create(&prod,NULL,producer,(void*)&i);
	pthread_create(&cons,NULL,consumer,(void*)&i);
	sem_init(&full,1,0);
	sem_init(&empty,1,5);
	sem_init(&mutex,1,1);
	pthread_join(prod,NULL);
	pthread_join(cons,NULL);
	
}
